import axios from 'axios'
// import { Toast } from 'vant'
import store from '../store'

const instance = axios.create({
  // baseURL: '/wxwork/wxws',
  timeout: 60 * 1000,
  headers: {
    sine_url: getUrl(), //  动态获取当前url，截取到index.html之前
    WX_REQUEST_CHANNEL: 'AXIOS_HTTP_REQUEST', // 固定
    WX_TOKEN: store.getters.getToken, // getCurrentUser 接口 取token字段
    WX_APP_ID: store.getters.getAppId // 由后台提供，每个项目不同
  }
})

function getUrl () {
  const url = location.href
  const _index = url.indexOf('#')
  if (_index !== -1) {
    return url.substring(0, _index)
  }
  return url
}

instance.interceptors.request.use(
  function (config) {
    config.headers.WX_TOKEN = store.getters.getToken
    return config
  },
  function (error) {
    return Promise.reject(error)
  }
)

instance.interceptors.response.use(
  function (response) {
    return response
  },
  function (error) {
    // console.log('err' + error)
    return Promise.reject(error)
  }
)

export default instance
